package jianzhiOffer;

public class num25 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode node1 = l1;
        ListNode node2 = l2;
        ListNode dummyHead = new ListNode(-1);
        ListNode cur = dummyHead;
        while(node1 != null && node2 != null) {
            if(node1.val <= node2.val) {
                ListNode node = new ListNode(node1.val);
                cur.next = node;
                cur = cur.next;
                node1 = node1.next;
            }else {
                ListNode node = new ListNode(node2.val);
                cur.next = node;
                cur = cur.next;
                node2 = node2.next;
            }
        }
        if(node1 == null) {
            while(node2 != null) {
                ListNode node = new ListNode(node2.val);
                cur.next = node;
                cur = cur.next;
                node2 = node2.next;
            }
        }
        if(node2 == null) {
            while(node1 != null) {
                ListNode node = new ListNode(node1.val);
                cur.next = node;
                cur = cur.next;
                node1 = node1.next;
            }
        }
        return dummyHead.next;
    }
}
